import type { Shape } from './shapes/Shape';
import type {
  AnchorPoint,
  CanvasState,
  DrawingMode,
  StateManagerOptions,
} from './types';

export class StateManager {
  private state: CanvasState;
  
  onStateChange?: (state: CanvasState) => void;

  constructor(options: StateManagerOptions = {}) {
    this.state = {
      mode: 'select',
      isDragging: false,
      isDrawing: false,
      isSelecting: false,
      isMovingShape: false,
      isResizing: false,
      isResizingShape: false,
      dragStartX: 0,
      dragStartY: 0,
      offsetX: 0,
      offsetY: 0,
      scale: 1,
      lastX: 0,
      lastY: 0,
      originX: 0,
      originY: 0,
      currentShape: null,
      shapes: [],
      selectedShape: null,
      strokeColor: '#0000ff',
      strokeWidth: 2,
      activeAnchor: null,
      ...options.initialState,
    };

    this.onStateChange = options.onStateChange;
  }

  // 获取当前状态
  getState(): CanvasState {
    return { ...this.state };
  }

  // 更新状态
  updateState(updates: Partial<CanvasState>): void {
    this.state = { ...this.state, ...updates };
    this.notifyStateChange();
  }

  // 设置绘图模式
  setMode(mode: DrawingMode): void {
    this.updateState({ mode });
  }

  getMode(): DrawingMode {
    return this.state.mode;
  }

  // 设置拖拽状态
  setDragging(isDragging: boolean, startX: number, startY: number): void {
    this.updateState({
      isDragging,
      dragStartX: startX,
      dragStartY: startY,
    });
  }

  // 设置绘图状态
  setDrawing(isDrawing: boolean, shape: Shape | null = null): void {
    this.updateState({
      isDrawing,
      currentShape: shape,
    });
  }

  // 设置移动形状状态
  setMovingShape(isMoving: boolean): void {
    this.updateState({ isMovingShape: isMoving });
  }

  // 设置调整大小状态
  setResizing(isResizing: boolean, anchor: AnchorPoint | null = null): void {
    this.updateState({
      isResizing,
      activeAnchor: anchor,
    });
  }

  // 设置调整形状状态（用于锚点调整）
  setResizingShape(isResizing: boolean, startX: number, startY: number): void {
    this.updateState({
      isResizingShape: isResizing,
      dragStartX: startX,
      dragStartY: startY,
    });
  }

  // 设置缩放
  setScale(scale: number): void {
    this.updateState({ scale });
  }

  // 设置偏移量
  setOffset(offsetX: number, offsetY: number): void {
    this.updateState({ offsetX, offsetY });
  }

  // 设置原点
  setOrigin(originX: number, originY: number): void {
    this.updateState({ originX, originY });
  }

  // 设置最后位置
  setLastPosition(x: number, y: number): void {
    this.updateState({ lastX: x, lastY: y });
  }

  // 设置笔触颜色
  setStrokeColor(color: string): void {
    this.updateState({ strokeColor: color });
  }

  // 设置笔触宽度
  setStrokeWidth(width: number): void {
    this.updateState({ strokeWidth: width });
  }

  // 添加形状
  addShape(shape: Shape): void {
    const shapes = [...this.state.shapes, shape];
    this.updateState({ shapes });
  }

  // 移除形状
  removeShape(shape: Shape): void {
    const shapes = this.state.shapes.filter((s) => s !== shape);
    this.updateState({ shapes });
  }

  // 清除所有形状
  clearShapes(): void {
    this.updateState({
      shapes: [],
      selectedShape: null,
      currentShape: null,
    });
  }

  // 选择形状
  selectShape(shape: Shape | null): void {
    if (this.state.selectedShape) {
      this.state.selectedShape.selected = false;
    }

    if (shape) {
      shape.selected = true;
    }

    this.updateState({ selectedShape: shape });
  }

  // 获取选中形状
  getSelectedShape(): Shape | null {
    return this.state.selectedShape;
  }

  // 获取所有形状
  getShapes(): Shape[] {
    return [...this.state.shapes];
  }

  // 状态变化通知
  private notifyStateChange(): void {
    if (this.onStateChange) {
      this.onStateChange(this.getState());
    }
  }

  // 重置状态
  reset(): void {
    this.updateState({
      mode: 'select',
      isDragging: false,
      isDrawing: false,
      isSelecting: false,
      isMovingShape: false,
      isResizing: false,
      dragStartX: 0,
      dragStartY: 0,
      offsetX: 0,
      offsetY: 0,
      scale: 1,
      lastX: 0,
      lastY: 0,
      originX: 0,
      originY: 0,
      currentShape: null,
      selectedShape: null,
      activeAnchor: null,
    });
  }
}

// 创建全局状态管理器实例
export const stateManager = new StateManager();
